package sequentiell;

import java.util.ArrayList;

/**
 * @author Lukas Subel, Patrick Bender
 *
 */
public class Node {

	/**
	 * The identifying id of the node.
	 */
	private int id;
	/**
	 * The capacity of the node.
	 * Used to find out which nodes are connected to other nodes.
	 */
	private int capacity;
	/**
	 * The list with the predecessors of the node.
	 */
	private ArrayList<Node> pred;
	/**
	 * The masche which contains this node.
	 */
	private Masche masche;
	
	/**
	 * The constructor of the node.
	 * Initialize the pred-list.
	 * 
	 * @param id the identifying number 
	 * @param capacity the capacity of the node
	 */
	public Node (int id, int capacity) {
		this.id = id;
		this.capacity = capacity;
		this.pred = new ArrayList<Node>();
	}
	
	/**
	 * The default contructor. No additional informations about id or capacity.
	 * Initialize the pred-list.
	 */
	public Node () {
		this.pred = new ArrayList<Node>();
	}
	
	/**
	 * Set the id of the node.
	 * @param id The identifying id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Set the capacity of the nod
	 * @param capacity The capacity
	 */
	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	/**
	 * Add a node the the list of predecessors.
	 * @param node The node to add. Checks if the argument is null.
	 */
	public void addPred (Node node) {
		if (node == null) throw new IllegalArgumentException("Null is not a valid Node");
		this.pred.add(node);
	}

	/**
	 * Give the list of the predecessors.
	 * @return The list of the predecessors.
	 */
	public ArrayList<Node> getPred() {
		return pred;
	}

	/**
	 * Set pred-list to a existing list. 
	 * @param pred The existing list. Checks if the argument is null.
	 */
	public void setPred(ArrayList<Node> pred) {
		if (pred == null) throw new IllegalArgumentException("Pred-list can't be null");
		this.pred = pred;
	}

	/**
	 * Return the masche which contains this node.
	 * @return The masche.
	 */
	public Masche getMasche() {
		return masche;
	}

	/**
	 * Set the masche of this node.
	 * @param masche The masche which contains now this node.
	 */
	public void setMasche(Masche masche) {
		this.masche = masche;
	}

	/**
	 * Get the ID of the node.
	 * @return The identifying ID.
	 */
	public int getId() {
		return id;
	}

	/**
	 * Get the capacity of the node.
	 * @return The capacity.
	 */
	public int getCapacity() {
		return capacity;
	}
	
	/**
	 * Helping method for the toSting() method; build a String about the predecessors of the node
	 * @return The String with the predecessors.
	 */
	public String predToString() {
		String erg = "{ ";
			for (Node act : pred)
			{
			erg += act.getId() + ", ";
			}
			if (pred.size() == 0) erg += "  ";
		return erg.subSequence(0, erg.length()-2) + " }";
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "{ " + id + ", " + capacity + ", " + predToString() + ", " + masche + " }";
	}
	
}
